package com.competition.front.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.competition.commonutils.JwtUtils;
import com.competition.commonutils.R;
import com.competition.front.entity.*;
import com.competition.front.entity.vo.ExaminationVo;
import com.competition.front.entity.vo.QueryVo;
import com.competition.front.entity.vo.UserRankVo;
import com.competition.front.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/front/ucenter-member")
@CrossOrigin
@Api(description = "关于用户相关的一些接口")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private ModuleService moduleService;

    @Autowired
    private ExaminationService examinationService;

    @Autowired
    private UserCompetitionService userCompetitionService;

    @Autowired
    private ExaminationDetailService examinationDetailService;

    @Autowired
    private UserAdditionalService userAdditionalService;

    @ApiOperation("展示总前十排名")
    @GetMapping("showRank")
    public R showRank(){
        List<UserRankVo> userRankVoList=userService.showRank();
        return R.ok().data("userRankVoList",userRankVoList);
    }

    @ApiOperation("用户更改")
    @PostMapping("updateUser")
    public R updateUser(@RequestBody User user, HttpServletRequest request){
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        user.setId(memberIdByJwtToken);
        boolean b = userService.updateById(user);
        return b==true?R.ok(): R.error();
    }

    @PostMapping("getUserModule/{relationshipId}")
    @ApiOperation("根据模块分类id获取用户发布的对应文章")
    public R getUserModule(@PathVariable String relationshipId,HttpServletRequest request){
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        QueryWrapper<Module> moduleQueryWrapper=new QueryWrapper<>();
        moduleQueryWrapper.eq("relationship_id",relationshipId);
        moduleQueryWrapper.eq("user_id",memberIdByJwtToken);
        List<Module> list = moduleService.list(moduleQueryWrapper);
        int count = moduleService.count(moduleQueryWrapper);
        return R.ok().data("count",count).data("list",list);
    }

    @ApiOperation("获取用户参加竞赛相关的信息")
    @GetMapping("getUserExamination")
    public R getUserExamination(HttpServletRequest request){
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        QueryWrapper<UserCompetition> userCompetitionQueryWrapper=new QueryWrapper<>();
        userCompetitionQueryWrapper.eq("user_id",memberIdByJwtToken);
        List<UserCompetition> userCompetitionList = userCompetitionService.list(userCompetitionQueryWrapper);//用户参与的全部竞赛
        List<ExaminationVo> examinationVoList=new ArrayList<>();//封装数据
        for (int i=0;i<userCompetitionList.size();i++){
            ExaminationVo examinationVo=new ExaminationVo();
            examinationVo.setStartTime(userCompetitionList.get(i).getCreateTime());//设置开始时间
            examinationVo.setUserScore(userCompetitionList.get(i).getScore());//设置用户分数
            String examinationId = userCompetitionList.get(i).getExaminationId();
            //封装该竞赛总分数与标题
            Examination examination = examinationService.getById(examinationId);
            examinationVo.setTotalScore(examination.getTopicScore());
            examinationVo.setExaminationName(examination.getExaminationName());
            //封装题目
            QueryWrapper<ExaminationDetail> examinationDetailQueryWrapper=new QueryWrapper<>();
            examinationDetailQueryWrapper.eq("examination_id",examinationId);
            List<ExaminationDetail> examinationDetailList = examinationDetailService.list(examinationDetailQueryWrapper);
            examinationVo.setExaminationDetailList(examinationDetailList);
            examinationVoList.add(examinationVo);
        }

        QueryWrapper<UserAdditional> userAdditionalQueryWrapper=new QueryWrapper<>();
        userAdditionalQueryWrapper.eq("user_id",memberIdByJwtToken);
        UserAdditional userAdditional = userAdditionalService.getOne(userAdditionalQueryWrapper);
        return R.ok().data("examinationVoList",examinationVoList).data("userAdditional",userAdditional);
    }




}

